<?php

namespace api;

use app\lib\api;
use app\lib\model\cate;
use app\lib\model\settings;
use app\lib\token;

/**
 * 后台接口
 * Class admin
 *
 * @package api
 */
class admin extends api
{
    public int $check_token = 2;

    public function admin_login(string $account, string $pwd)
    {
        if ($account == 'admin' && $pwd == 'admin1010') {
            $token_admin = token::new()->make(['id' => 1, 'tk_key' => 'admin'], 'id');
            return ['token_admin' => $token_admin];
        }
        $this->fail(10001);
    }

    public function menu_list()
    {
        if ($this->account_id == 0) {
            $this->fail(10010);
        }
        return [
            ['icon' => 'el-icon-s-data',
                'index' => '/game',
                'title' => '游戏列表',],
            ['icon' => 'el-icon-share',
                'index' => '/cate',
                'title' => '游戏类别',],
            ['icon' => 'el-icon-share',
                'index' => '/settings',
                'title' => '其他设置',],
        ];

    }

    /**
     * 游戏列表
     *
     * @param int $page
     * @param int $page_size
     *
     * @return array
     */
    public function game_list(int $page, int $page_size, string $keyword = '')
    {
        $model = \app\lib\model\game::new();
        if ($keyword) {
            $model->where(['title', 'like', '%' . $keyword . '%']);
        }
        $res = [
            'curr_page' => $page,
            'cnt_data' => 0,
            'cnt_page' => 0,
            'list' => [],
            'tab_head' => [
                ['name' => 'ID', 'key' => 'game_id'],
                ['name' => '名称', 'key' => 'title'],
                ['name' => '分类', 'key' => 'cate_title'],
                ['name' => '简介', 'key' => 'desc'],
            ]
        ];
        $res['cnt_data'] = $model->cnt();
        $res['cnt_page'] = ceil($res['cnt_data'] / $page_size);
        if ($keyword) {
            $model->where(['a.title', 'like', '%' . $keyword . '%']);
        }
        $res['list'] = $model->alias("a")->join("cate b", 'left')->on(['a.cate_id', 'b.cate_id'])->limit(($page - 1) * $page_size, $page_size)->read_all('game_id,a.title,`desc`,b.title as cate_title,b.cate_id');
        return $res;
    }

    /**
     * 添加或编辑
     *
     * @return bool
     */
    public function update_game(int $cate_id, string $title, string $desc, int $game_id = 0)
    {
        $model = \app\lib\model\game::new();
        if ($game_id) {
            return $model->where(['game_id', $game_id])->save_up(['title' => $title, 'cate_id' => $cate_id, '`desc`' => $desc]);
        }
        return $model->save(['title' => $title, 'cate_id' => $cate_id, '`desc`' => $desc]);
    }

    /**
     * 删除游戏
     *
     * @param int $game_id
     *
     * @return bool
     */
    public function del_game(int $game_id)
    {
        return \app\lib\model\game::new()->where(['game_id', $game_id])->del();
    }

    /**
     * 分类列表
     *
     * @param int $page
     * @param int $page_size
     *
     * @return array
     */
    public function cate_list(int $page, int $page_size, string $keyword = '')
    {
        $model = cate::new();
        if ($keyword) {
            $model->where([['title', 'like', '%' . $keyword . '%']]);
        }
        $res = [
            'curr_page' => $page,
            'cnt_data' => 0,
            'cnt_page' => 0,
            'list' => [],
            'tab_head' => [
                ['name' => 'ID', 'key' => 'cate_id'],
                ['name' => '名称', 'key' => 'title'],
                ['name' => '排序', 'key' => 'sort'],
            ]
        ];
        $res['cnt_data'] = $model->cnt();
        $res['cnt_page'] = ceil($res['cnt_data'] / $page_size);
        $res['list'] = $model->order(['sort' => 'desc'])->limit(($page - 1) * $page_size, $page_size)->read_all('cate_id,title,sort');
        return $res;
    }

    /**
     * 添加或编辑
     *
     *
     * @return bool
     */
    public function update_cate(string $title, int $sort, int $cate_id = 0)
    {
        $model = cate::new();
        if ($cate_id) {
            return $model->where(['cate_id', $cate_id])->save_up(['title' => $title, 'sort' => $sort]);
        }
        return $model->save(['title' => $title, 'sort' => $sort]);
    }

    /**
     * 删除分类
     *
     * @param int $cate_id
     *
     * @return bool
     */
    public function del_cate(int $cate_id)
    {
        return cate::new()->where(['cate_id', $cate_id])->del();
    }

    public function set_text(int $type, string $text)
    {
        $model = settings::new();
        return $model->where(['type', $type])->save_up(['text' => $text]);
    }

}
